﻿using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Security;
using System;
using Vitus.Featured.Models;
using Vitus.Featured.Settings;

namespace Vitus.Featured.Drivers
{
    public class FeaturedPartDriver : ContentPartDriver<FeaturedPart>
    {
        private IAuthorizer authorizer;

        public FeaturedPartDriver(IAuthorizer authorizer)
        {
            this.authorizer = authorizer;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override string Prefix { get { return "Featured"; } }

        protected override DriverResult Display(FeaturedPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_Featured_Body",
                () => shapeHelper.Parts_Featured_Body(ContentPart: part));
        }

        protected override DriverResult Editor(FeaturedPart part, dynamic shapeHelper)
        {
            if (!authorizer.Authorize(FeaturedPermissions.ManageFeatured))
            {
                return null;
            }

            return ContentShape("Parts_Featured_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Featured",
                    Model: part,
                    Prefix: Prefix));
        }

        protected override DriverResult Editor(FeaturedPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            if (!authorizer.Authorize(FeaturedPermissions.ManageFeatured))
            {
                return null;
            }

            updater.TryUpdateModel(part, Prefix, null, null);

            if (part.IsFeatured == true)
            {
                var settings = part.TypePartDefinition.Settings.GetModel<FeaturedSettings>();

                if (settings.TitlePartUsageMode == FieldDependencyMode.Disallow
                    && string.IsNullOrEmpty(part.Title))
                {
                    updater.AddModelError("Featured.Title", T("Featured title is required"));
                }

                if (settings.TitleMaxLength != 0
                    && part.Title != null
                    && part.Title.Length > settings.TitleMaxLength)
                {
                    updater.AddModelError("Featured.Title", new LocalizedString(string.Format(T("Featured title must not be longer than {0} characters").ToString(), settings.TitleMaxLength)));
                }

                if (settings.BodyPartUsageMode == FieldDependencyMode.Disallow
                    && string.IsNullOrEmpty(part.Body))
                {
                    updater.AddModelError("Featured.Body", T("Featured body text is required"));
                }

                if (settings.BodyMaxLength != 0
                    && part.Body != null
                    && part.Body.Length > settings.BodyMaxLength)
                {
                    updater.AddModelError("Featured.Body", new LocalizedString(string.Format(T("Featured body text must not be longer than {0} characters").ToString(), settings.BodyMaxLength)));
                }
            }
            
            return Editor(part, shapeHelper);
        }

        protected override void Importing(FeaturedPart part, ImportContentContext context)
        {
            var isFeatured = context.Attribute(part.PartDefinition.Name, "IsFeatured");

            if (isFeatured != null)
            {
                part.IsFeatured = bool.Parse(isFeatured);
            }

            var title = context.Attribute(part.PartDefinition.Name, "Title");

            if (title != null)
            {
                part.Title = title;
            }

            var body = context.Attribute(part.PartDefinition.Name, "Body");

            if (body != null)
            {
                part.Body = body;
            }

            var isPinned = context.Attribute(part.PartDefinition.Name, "IsPinned");

            if (isPinned != null)
            {
                part.IsPinned = bool.Parse(isPinned);
            }
        }

        protected override void Exporting(FeaturedPart part, ExportContentContext context)
        {
            context.Element(part.PartDefinition.Name).SetAttributeValue("IsFeatured", part.Record.IsFeatured);
            context.Element(part.PartDefinition.Name).SetAttributeValue("Title", part.Record.Title);
            context.Element(part.PartDefinition.Name).SetAttributeValue("Body", part.Record.Body);
            context.Element(part.PartDefinition.Name).SetAttributeValue("IsPinned", part.Record.IsPinned);
        }
    }
}
